package com.zjr.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zjr.domain.User;
import com.zjr.service.DepartmentService;

/**
 * Servlet implementation class DepartmentSerlvet
 */
@WebServlet("/DepartmentSerlvet")
public class DepartmentSerlvet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DepartmentSerlvet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String flag = request.getParameter("flag");
		DepartmentService ds = null;
		if(flag.equals("allDepartments")){
			ds = new DepartmentService();
			HashMap<String, Integer> departmentNum = ds.getDepartmentNum();
			request.setAttribute("departmentNum", departmentNum);
			request.getRequestDispatcher("/department/department_list.jsp")
				.forward(request, response);
		}else if(flag.equals("find")){
			ds = new DepartmentService();
			int did = Integer.parseInt(request.getParameter("did"));
			ArrayList<User> users = ds.getUserByDid(did);
			request.setAttribute("users", users);
			request.getRequestDispatcher("/department/department_user_list.jsp")
				.forward(request, response);
		}
	}

}
